﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InfoSource.Data;
using System.Web.Script.Serialization;
using System.Diagnostics;

namespace InfoSource.Models.Views
{
	public class ZipCodeLookupViewModel
	{
		#region Properties ======================================================================

		private InfoSourceDB _infoSourceDB { get; set; }

		public Dictionary<string,string> TypeAheadZipCodes { get; set; }

		public Dictionary<string, ZipCodeContratractorsModel> ZipCodeContractorDetails { get; set; }

		public string ZipCodeContractorDetailsSerialized
		{
			get
			{
				Dictionary<string, object> simplifiedZipCodeContractorDetails = new Dictionary<string, object>();

				foreach (KeyValuePair<string, ZipCodeContratractorsModel> kvp in this.ZipCodeContractorDetails)
				{				
					string key = kvp.Key;
					int zipCodeNumber = int.Parse(key);
					colorado_zipcode zipCode = _infoSourceDB.colorado_zipcodes.Where( COZip => COZip.zip == zipCodeNumber ).FirstOrDefault();
					List<object> servicePlanHVACContractors = kvp.Value.ServicePlanHVACContractors.Select(c => (object)(new {Code = c.Code, Name = c.Name})).ToList();
					List<object> servicePlanApplianceContractors = kvp.Value.ServicePlanApplianceContractors.Select(c => (object)(new {Code = c.Code, Name = c.Name})).ToList();
					List<object> brokeringHVACContractors = kvp.Value.BrokeringHVACContractors.Select(c => (object)(new {Code = c.Code, Name = c.Name})).ToList();
					List<object> brokeringApplianceContractors = kvp.Value.BrokeringApplianceContractors.Select(c => (object)(new {Code = c.Code, Name = c.Name})).ToList();
					List<object> miscellaneousContractors = kvp.Value.MiscellaneousContractors.Select(c => (object)(new {Code = c.Code, Name = c.Name})).ToList();
					Dictionary<string, object> zipDetails = new Dictionary<string, object>(){
						{ "COD", zipCode.cod },
						{ "Special", zipCode.special },
						{ "MetroZoneId", zipCode.MetroZoneID },
						{ "Zone", zipCode.zone },
						{ "Lat", zipCode.Latitude },
						{ "Lng", zipCode.Longitude }
					};

					simplifiedZipCodeContractorDetails.Add(
						key,
						new
						{
							ServicePlanHVACContractors = servicePlanHVACContractors,
							ServicePlanApplianceContractors = servicePlanApplianceContractors,
							BrokeringHVACContractors = brokeringHVACContractors,
							BrokeringApplianceContractors = brokeringApplianceContractors,
							MiscellaneousContractors = miscellaneousContractors,
							ZipcodeDetails = zipDetails
						});
				}
					
				return new JavaScriptSerializer().Serialize(simplifiedZipCodeContractorDetails);
			}
		}


		#endregion

		#region Constructor =====================================================================

		public ZipCodeLookupViewModel()
		{
			_infoSourceDB = new InfoSourceDB();

			SetTypeAheadZipCodes();

			SetZipCodeContractorDetails();
		}

		#endregion

		#region Methods =========================================================================

		public void SetTypeAheadZipCodes()
		{
			var zipcodes = _infoSourceDB.colorado_zipcodes.ToDictionary(COZip => COZip.zip.ToString(), COZip => COZip.city);

			this.TypeAheadZipCodes = zipcodes;
		}

		/*-------------------------------------------------------------------------*/
		public void SetZipCodeContractorDetails()
		{
			Dictionary<string, ZipCodeContratractorsModel> zipContractorDetails = new Dictionary<string, ZipCodeContratractorsModel>();

			foreach (int zipcode in _infoSourceDB.colorado_zipcodes.Select(COZip => COZip.zip))
			{
				ZipCodeContratractorsModel zipCodeContractorsModel = new ZipCodeContratractorsModel(zipcode);

				zipContractorDetails.Add(zipcode.ToString(), zipCodeContractorsModel);
			}

			this.ZipCodeContractorDetails = zipContractorDetails;
		}

		#endregion


	}
}